﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;

namespace Clock2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }
        public delegate void Fun();

        public MyClock myclock = new MyClock();

        public void run()
        {
            ListBox.Items.Clear();
            myclock.SendMsg += str =>
            {
                ListBox.BeginUpdate();
                ListBox.Items.Add(str);
                ListBox.EndUpdate();
                ListBox.SelectedIndex = ListBox.Items.Count - 1;
            };
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            int hour = DateTime.Now.Hour;
            int minute = DateTime.Now.Minute;
            if(int.TryParse(SetHour.Text,out hour) && int.TryParse(SetMinute.Text,out minute))
            {
                hour = (hour + DateTime.Now.Hour + (minute + DateTime.Now.Minute) / 60) % 24;
                minute = (minute + DateTime.Now.Minute) % 60;
                myclock = new MyClock(hour, minute); 
            }
            run();
            new Thread(myclock.Tick).Start();
        }

        private void btn_Start_min1_Click(object sender, EventArgs e)
        {
            int quickTime = 1;
            int hour = (DateTime.Now.Hour + (quickTime + DateTime.Now.Minute) / 60) % 24;
            int minute = (quickTime + DateTime.Now.Minute) % 60;
            myclock = new MyClock(hour, minute);
            run();
            new Thread(myclock.Tick).Start();
        }

        private void btn_Start_min5_Click(object sender, EventArgs e)
        {
            int quickTime = 5;
            int hour = (DateTime.Now.Hour + (quickTime + DateTime.Now.Minute) / 60) % 24;
            int minute = (quickTime + DateTime.Now.Minute) % 60;
            myclock = new MyClock(hour, minute);
            run();
            new Thread(myclock.Tick).Start();
        }

        private void btn_Start_min10_Click(object sender, EventArgs e)
        {
            int quickTime = 10;
            int hour = (DateTime.Now.Hour + (quickTime + DateTime.Now.Minute) / 60) % 24;
            int minute = (quickTime + DateTime.Now.Minute) % 60;
            myclock = new MyClock(hour, minute);
            run();
            new Thread(myclock.Tick).Start();
        }
    }

    public class MyClock
    {
        public int DueHour { set; get; }
        public int DueMinute { set; get; }
        public bool Alarmed = false;
        public DateTime dateTime = DateTime.Now;
        public event Action<string> SendMsg;
        public MyClock(int DueHour, int DueMinute)
        {
            this.DueHour = DueHour;
            this.DueMinute = DueMinute;
        }
        public MyClock(int DueTime)
        {
            this.DueHour = (dateTime.Hour + (dateTime.Minute + DueTime) / 60) % 24;
            this.DueMinute = (dateTime.Minute + DueTime) % 60;
        }
        public MyClock()
        {
            this.DueHour = (dateTime.Hour + (dateTime.Minute + 2) / 60) % 24;
            this.DueMinute = (dateTime.Minute + 2) % 60;
        }
        public void Tick()
        {
            while (!Alarmed)
            {
                if ( DateTime.Now.Hour <= DueHour && DateTime.Now.Minute < DueMinute )
                {
                    SendMsg("\n Tick Tick Tick");
                }
                else if(DueHour == DateTime.Now.Hour && DueMinute == DateTime.Now.Minute)
                {
                    Alarmed = true;
                    SendMsg ("\n 猪头，起床听课啦");
                }
                Thread.Sleep(1000);
            }
        }

    }
}
